import 'package:aero_frisbee/app/model/local_post_model/local_post_model.dart';
import 'package:aero_frisbee/app_route.dart';
import 'package:aero_frisbee/gen/assets.gen.dart';
import 'package:aero_frisbee/pages/modues/home/view_model/af_home_view_model.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';

class ProfileDetailPostCell extends ConsumerWidget {
  const ProfileDetailPostCell(this.item, {super.key});
  final LocalPostModel item;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        context.pushNamed(RouteUri.postDetail, extra: item);
      },
      child: Container(
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8),
          border: Border.all(
            color: const Color(0xffCCCCCC),
            width: 0.5,
          ),
        ),
        child: Column(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(8),
              child: Stack(children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(12),
                  child: AspectRatio(
                    aspectRatio: 162 / 177,
                    child: Image.asset(
                      'assets/res/${item.postCover ?? ''}',
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ]),
            ),
            Expanded(
              child: Padding(
                padding: const EdgeInsets.only(left: 10, right: 10),
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Row(
                      children: [
                        Expanded(
                          child: Text(
                            item.title ?? '',
                            style: const TextStyle(
                              color: Color(0xff141414),
                              fontWeight: FontWeight.normal,
                              fontSize: 14,
                            ),
                            textAlign: TextAlign.left,
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                          ),
                        )
                      ],
                    ),
                    const SizedBox(
                      height: 5,
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Flexible(
                          child: Row(
                            children: [
                              SizedBox(
                                height: 20,
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(10),
                                  child: AspectRatio(
                                    aspectRatio: 1,
                                    child: CachedNetworkImage(
                                      imageUrl:
                                          item.cacheBind?.homeUser?.avatar ??
                                              '',
                                      fit: BoxFit.fill,
                                      placeholder: (context, url) =>
                                          const Material(
                                        color: Color(0xffF2F5F9),
                                        child: Center(
                                          child: SizedBox(
                                            width: 25,
                                            height: 25,
                                            child: CircularProgressIndicator(
                                              strokeWidth: 2,
                                              color: Color(0xffADADAD),
                                              backgroundColor: Colors.white,
                                            ),
                                          ),
                                        ),
                                      ),
                                      errorWidget: (context, url, error) =>
                                          const CircularProgressIndicator(
                                        strokeWidth: 2,
                                        color: Color(0xffADADAD),
                                        backgroundColor: Colors.white,
                                      ),
                                    ),
                                  ),
                                ),
                              ),
                              Flexible(
                                child: Padding(
                                  padding:
                                      const EdgeInsets.symmetric(horizontal: 5),
                                  child: Text(
                                    item.cacheBind?.homeUser?.nickname ?? '',
                                    style: const TextStyle(
                                      color: Color(0xff404040),
                                      fontWeight: FontWeight.normal,
                                      fontSize: 10,
                                    ),
                                    textAlign: TextAlign.center,
                                    overflow: TextOverflow.ellipsis,
                                    maxLines: 1,
                                  ),
                                ),
                              ),
                            ],
                          ),
                        ),
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            ref
                                .read(afHomePostListServiceProvider.notifier)
                                .toggleLike(item.uid ?? 0);
                          },
                          child: item.isLike
                              ? Assets.icons.iconLikeLight.image(width: 15)
                              : Assets.icons.iconLikeGray.image(width: 15),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
